﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Touch
{
    public partial class Categories : System.Web.UI.Page
    {
        LocalFunctions FUNC;

        protected void Page_Load(object sender, EventArgs e)
        {
            FUNC = new LocalFunctions();
             DataSet DSCategories = FUNC.GetCategories();
             if (DSCategories != null)
             {
                 if (DSCategories.Tables[0] != null)
                     if (DSCategories.Tables[0].Rows.Count > 0)
                     {
                         var TopCategoriesList = from item in DSCategories.Tables[0].AsEnumerable().Select(
                                                 d => new Category
                                                 {
                                                     id = Convert.ToInt32(d["id"]),
                                                     parentid = Convert.ToInt32((d["parentid"] == string.Empty) ? "0" : d["parentid"]),
                                                     name = d["name"].ToString()
                                                 })
                                                 select item;

                         List<Category> Cats = new List<Category>();
                         for (int i = 0; i < TopCategoriesList.ToList<Category>().Count; i++)
                         {

                             Category c = TopCategoriesList.ToList<Category>()[i];
                             //if (c.id == 19)
                             //    c.link = "http://3g.orange.co.il/tablet/Category.aspx?id=2804710";
                             //else
                                 c.link = string.Format("Category.aspx?id={0}", c.id);

                             Cats.Add(c);
                         }

                         #region First
                         rptCategories.DataSource = Cats;
                         rptCategories.DataBind();
                         #endregion
                     }
             }
        }
    }
}
